
import 'dart:convert';

import 'package:dio/dio.dart' as dio;

import 'dart:io';
import 'dart:typed_data';
import 'package:chewie/chewie.dart';
import 'package:crypto/crypto.dart';
import 'package:dotted_border/dotted_border.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_image_compress/flutter_image_compress.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'package:images_picker/images_picker.dart';
import 'package:multi_image_picker_view/multi_image_picker_view.dart';
import 'package:qianyi/controller/homeTabController.dart';
import 'package:qianyi/entity/home/tag_vo_list_entity.dart';
import 'package:qianyi/network/base_entity.dart';
import 'package:qianyi/network/http_entity.dart';
import 'package:qianyi/network/http_manager.dart';
import 'package:qianyi/state/token.dart';
import 'package:qianyi/utils/color_util.dart';
import 'package:qianyi/wcao/kit/index.dart';
import 'package:qianyi/wcao/ui/theme.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:video_compress_plus/video_compress_plus.dart';
import 'package:video_thumbnail/video_thumbnail.dart';


import 'package:http/http.dart' as http;

import '../../../../network/http_config.dart';
import '../../../../ui/picker/custom/full_custom_example.dart';
import '../../../../utils/throttle.dart';
import '../../entity/home/alumb_vo_list.dart';

class AddAlbumViewToMe extends StatefulWidget {
  const AddAlbumViewToMe({Key? key}) : super(key: key);

  @override
  _addalbumViewState createState() => _addalbumViewState();
}
class _addalbumViewState extends State<AddAlbumViewToMe> with AutomaticKeepAliveClientMixin<AddAlbumViewToMe> ,SingleTickerProviderStateMixin{
  TextEditingController textEditingController = TextEditingController();
  HomeTabController homeTabController = Get.find();
  TokenController tokenController = Get.find();
  AlbumVoList? trendEntityList;
  int selectIndex = -1;
  String id = "";
  @override
  void initState() {
    // TODO: implement initState
   getUserAlbum();

  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(
        title: Text("收藏至专辑"),

      ),
      bottomNavigationBar: PreferredSize(
        preferredSize: Size.fromHeight(50.0), // 指定固定高度
        child: BottomAppBar(
        // color: Colors.blue,
        child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceEvenly,
        children: <Widget>[

          MaterialButton(

            //背景颜色
            color: WcaoTheme.primaryOpacity,
            //边框样式
            shape: const RoundedRectangleBorder(
              //边框颜色
              side: BorderSide(
                color: Color(0xffe4e4e4),
                width: .5,
              ),
              //边框圆角
              borderRadius: BorderRadius.all(
                Radius.circular(8),
              ),
            ),
            //按钮高度
            height: 32,
            //按钮最小宽度
            minWidth: 60,
            //点击事件
            onPressed: () async {

            Map<String,dynamic> params = {

              "aid": id,
              "mid": homeTabController.setAddAlbumImageId,


              "uid":tokenController.uid
            };
            HttpEntity h1 = await HttpManager.getInstance().request(url: '/platform/albumImgRelation/addAlbumImgRelation',params: params,method: Method.post) as HttpEntity;
            HudUtil.dismiss();
            Navigator.pop(context);
            },
            child:  Text(
              "添加至专辑",
              style: TextStyle(
                color: WcaoTheme.bgColor
              ),
            ),
          )
    // 其他按钮...
      ],
      ),
      ),
      ),
      body: Padding(
        padding: EdgeInsets.all(10),
        child: ListView(
          children: [

            trendEntityList!=null?
            Column(
              children: trendEntityList!.albumVos!
                  .asMap()
                  .map((index, e) => MapEntry(index, createItemWithIndex(e, index)))
                  .values
                  .toList() as List<Widget>,
            ):Text(""),
         Container(
          height: 15,
          width: MediaQuery.of(context).size.width - 10,
          decoration: BoxDecoration(
          border:Border(bottom:BorderSide(width: 1,color: Color(0xffe5e5e5)) )
      //              border: Border.all(color: Colors.grey,width: 0.5),

      //              borderRadius: BorderRadius.circular(5),
          )),



          ],
        ),
      ),
    );
  }
  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => false;

  Future<void> getUserAlbum() async {
    SharedPreferences prefs = await SharedPreferences.getInstance();
    Map<String,dynamic> userInfo = jsonDecode(prefs.getString("userInfo")!);


    Map<String, dynamic> params = {};
    params["uid"] = userInfo["id"];

    IBaseEntity? result = await HttpManager.getInstance().request(url: "/platform/album/getAllAlbum",method: Method.get, params: params);
    HttpEntity httpEntity = result as HttpEntity;
    trendEntityList = AlbumVoList.fromJson(httpEntity.oriData['data']);
    setState(() {

    });
  }

  Widget createItemWithIndex(AlbumVo e, int index) {
    return GestureDetector(
      onTap: () {
        this.selectIndex = index;
        this.id = e.id!;
        setState(() {

        });
      },
      child:Container(

        decoration: BoxDecoration(
            borderRadius: BorderRadius.circular(8), // 示例半径值，您可以调整它
            color: selectIndex == index ? Color(0xffcccccc).withOpacity(.8):Color(0xffcccccc).withOpacity(.1)
        ),
        padding: EdgeInsets.all(10), // 添加内边距，以便文本周围有空间
        child: Center(
          child: Text('${e.name}'),
        ),
      ),
    );
  }


}